<?php

namespace App\Services\Cart;

use App\Services\BaseServices;
use App\Services\Goods\GoodsServices;
use Illuminate\Support\Facades\Redis;

class CartRedisHashServices extends BaseServices
{
    /**
     * 模拟加入购物车 redisHash
     * @param  int  $goodsId
     * @param $userId
     * @param  int  $num
     * @return bool
     */
    public function saveCart(int $goodsId, $userId, int $num, $sku = '')
    {
        if (empty($goodsId)) {
            return false;
        }
        $goods = GoodsServices::getInstance()->getGoodsById($goodsId);
        if (is_null($goods)) {
            return "商品已下架";
        }
        $is = Redis::hGet('cart_user_'.$userId, $goodsId);
        if (!empty($is)) {
            return "该商品已再购物车中";
        }
        $goods = $goods->toArray();
        $data = [
            'title' => $goods['goodsTitle'],
            'price' => $goods['price'],
            'goodsId' => $goods['id'],
            'num' => $num,
            'sku' => $sku,
            'addTime' => time()
        ];
        $ret = Redis::hSet("cart_user_".$userId, $goodsId, json_encode($data));
        if ($ret == 0) {
            return "添加失败";
        }
        return true;
    }

    /**
     * 模拟获取redisHash购物车列表或
     * @param $userId
     * @param $goodsId
     * @param  string  $type
     * @return array|false
     */
    public function getCartListAllOrById($userId, $goodsId, $type = "all")
    {
        if (empty($userId) || empty($goodsId)) {
            return false;
        }
        if ($type == "all") {
            $cart = Redis::hgetall("cart_user_".$userId);
            $ids = array_keys($cart);
            $goods = GoodsServices::getInstance()->getGoodsInId($ids);
            if (is_null($goods)) {
                return false;
            }
            $goods = $goods->keyBy('id')->toArray();
            foreach ($cart as $key => $value) {
                $cart[$key] = json_decode($value, true);
                $cart[$key]['price'] = $goods[$key]['price'];
                $cart[$key]['price_all'] = bcmul($cart[$key]['num'], $cart[$key]['price'], 2);
                $cart[$key]['sku'] = GoodsServices::getInstance()->goodsSku($key);
            }
        } elseif ($type == "id") {
            $Cart = json_decode(Redis::hGet("cart_user_".$userId, $goodsId), true);
            $goods = GoodsServices::getInstance()->getGoodsById($goodsId);
            if ($Cart['price'] != $goods['price']) {
                $Cart['price'] = $goods['price'];
            }
            $Cart['price'] = bcmul($Cart['num'], $Cart['price'], 2);
        } elseif ($type == "ids") {
            $redisCart = Redis::hgetall("cart_user_".$userId);
            $ids = explode(",", $goodsId);
            $goods = GoodsServices::getInstance()->getGoodsInId($ids);
            if (is_null($goods)) {
                return false;
            }
            $goods = $goods->keyBy('id')->toArray();
            foreach ($ids as $value) {
                $redisCart[$value] = json_decode($redisCart[$value], true);
                $cart[$value]['price'] = $goods[$value]['price'];
                $cart[$value]['title'] = $redisCart[$value]['title'];
                $cart[$value]['num'] = $redisCart[$value]['num'];
                $cart[$value]['goodsId'] = $redisCart[$value]['goodsId'];
                $cart[$value]['addTime'] = $redisCart[$value]['addTime'];
                $cart[$value]['sku'] = GoodsServices::getInstance()->goodsSku($value);
            }
        }
        if (empty($cart)) {
            return false;
        }
//        排序
        array_multisort(array_column($cart, "addTime", "goodsId"), SORT_DESC, $cart);
        return $cart;
    }

    /**
     *
     * @param $userId
     * @param $goodsId
     * @param  string  $type
     * @return bool
     */
    public
    function delCartAllOrById(
        $userId,
        $goodsId,
        $type = "id"
    ) {
        if (empty($goodsId) || empty($userId)) {
            return false;
        }
        $cart = Redis::hGet("cart_user_".$userId, $goodsId);
        if (!$cart) {
            return false;
        }
        if ($type == "all") {
            $ret = Redis::hDel("cart_user_".$userId);
        } else {
            $ret = Redis::hDel("cart_user_".$userId, $goodsId);
        }

        if (!$ret) {
            return false;
        }
        return true;
    }

    /**
     *
     * @param $userId
     * @param $goodsId
     * @param $num
     * @return bool
     */
    public
    function updCart(
        $userId,
        $goodsId,
        $num,
        $sku = ''
    ) {
        //todo 修改sku属性值
        if (empty($goodsId) || empty($userId)) {
            return false;
        }
        $isNull = Redis::hGet("cart_user_".$userId, $goodsId);
        if (is_null($isNull)) {
            return false;
        }
        $ret = $this->delCartAllOrById($userId, $goodsId);
        if (!$ret) {
            return false;
        }
        $res = $this->saveCart($goodsId, $userId, $num, $sku);
        if (!$res) {
            return false;
        }
        return true;
    }
}
